"""
Case Type   : gs_checkse工具功能
Case Name   : 验证gs_checkse的A1检测项确保对外服务端口使用非默认端口号检测与修复成功
Create At   : 2024/10/23
Owner       : lihongji
Description :
    1.设置数据库端口号为默认端口号5432后检测A1项
    2.修复A1不符合安全规范项
    3.gs_guc修复后再次检测数据库端口项
    4.gsql登入数据库后查看数据库端口号
Expect      :
    1.修改端口成功，提示数据库端口不应使用默认端口
    2.提示端口号不能通过工具修复
    3.修复成功且不打印端口号告警信息
    4.端口号非5432
History     :
"""
import os
import unittest

from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from yat.test import Node
from yat.test import macro


class GS_CHECKSE(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.primaryNode = Node('PrimaryDbUser')
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.common = Common()
        self.env_path = macro.DB_ENV_PATH

    def test_gscheckse(self):
        step = ('----step1:设置数据库端口号为默认端口号5432后检测A1项'
                'expect:成功----')
        self.log.info(step)
        excute_msg = self.sh_primary.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG,
            "port=5432")
        self.assertTrue(excute_msg)
        restart_msg = self.sh_primary.restart_db_cluster()
        self.assertTrue(restart_msg)
        gs_checkse_cmd = (f'source {self.env_path};'
                          f'gs_checkse -i A1 --detail')
        self.log.info(gs_checkse_cmd)
        check_res = self.common.get_sh_result(self.primaryNode, gs_checkse_cmd)
        self.assertIn("service ports use non-default port numbers",
                      check_res, '执行失败' + step)

        step = '----step2:修复A1不符合安全规范项 expect:提示端口号不能通过工具修复----'
        self.log.info(step)
        gs_checkse_cmd = (f'source {self.env_path};'
                          f'gs_checkse -i B1 --detail;')
        self.log.info(gs_checkse_cmd)
        check_res = self.common.get_sh_result(self.primaryNode, gs_checkse_cmd)
        self.assertIn("NOTICE: Port value and some warning items can"
                         " NOT be set. Please do it manually.", check_res,
                         '执行失败' + step)

        step = '----step3:gs_guc修复后再次检测数据库端口项 expect:成功----'
        self.log.info(step)
        excute_msg = self.sh_primary.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG,
            f'port={self.primaryNode.db_port}')
        self.assertTrue(excute_msg)
        restart_msg = self.sh_primary.restart_db_cluster()
        self.assertTrue(restart_msg)
        gs_checkse_cmd = (f'source {self.env_path};'
                          f'gs_checkse -i A1 --detail')
        self.log.info(gs_checkse_cmd)
        check_res = self.common.get_sh_result(self.primaryNode, gs_checkse_cmd)
        self.assertNotIn("service ports use non-default port numbers",
                         check_res, '执行失败' + step)

        step = '----step4:gsql登入数据库后查看数据库端口号 expect:端口号非5432----'
        self.log.info(step)
        check_sql = """SELECT name,setting FROM pg_settings WHERE 
        name = 'port' AND setting = '5432';"""
        create_res = self.sh_primary.execut_db_sql(check_sql)
        self.log.info(create_res)
        self.assertIn('(0 rows)', create_res, '执行失败' + step)

    def tearDown(self):
        step = '----step5:无需清理环境 expect:成功----'
        self.log.info(step)
        self.log.info(f'----{os.path.basename(__file__)} end----')